// C Source File
// Created 11/19/2007; 9:05:29 PM

#include "Technology.h"
#include "Civilization.h"

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// TechWindow /////////////////////////////////////////////////////////////////////////////////////////////////////
// gets next tech to research /////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
char TechWindow(char civ)
{
	char i, j = 0;
	char nextTechs[10];
	
	for(i = 0; i < TECH_NUMBER; i++)
	{
		if(!GetBit(civilization[civ].techs, i))
		{
			if((techs[i].preq1 == -1 || GetBit(civilization[civ].techs, techs[i].preq1)) &&
				 (techs[i].preq2 == -1 || GetBit(civilization[civ].techs, techs[i].preq2)))
			{
				nextTechs[j++] = i;
				//DebugStr(techs[i].name);
			}
		}
	}
	
	// Future Tech
	if(j == 0) { nextTechs[j++] = 89; return 89; }
	
	if(civ != 0)
	{
		int iMax = 0;
		for(i = 1; i < j; i++)
			if(techs[nextTechs[iMax]].aiValue < techs[nextTechs[i]].aiValue && random(3) > 0)
				iMax = i;
				
		return nextTechs[iMax];
	}
	
	DrawLine(20, 10, 140, 10, A_NORMAL);
	DrawLine(20, 81, 140, 81, A_NORMAL);
	DrawLine( 20, 10,  20, 81, A_NORMAL);
	DrawLine(140, 10, 140, 81, A_NORMAL);
	
	char k = 0;
	int key = 0;
	while(key != KEY_ENTER && key != KEY_ESC)
	{
		Clr(21, 11, 139, 79);
		DrawLine(20, 20, 140, 20, A_NORMAL);
		
		FontSetSys(F_6x8);
		DrawStr(22, 12, techs[nextTechs[k]].name, A_NORMAL);
		FontSetSys(F_4x6);
		
		char a = 0, b;
		DrawStr(22, 23 + 8*a++, "Buildings", A_NORMAL);
		for(b = 0; b < BUILDING_NUMBER; b++)
			if(buildings[b].preq == nextTechs[k])
				DrawStr(25, 23 + 8*a++, buildings[b].name, A_NORMAL);
		if(a == 1) DrawStr(25, 23 + 8*a++, "None", A_NORMAL);
		a++;
		
		DrawStr(22, 23 + 8*a++, "Units", A_NORMAL);
		char c = a;
		for(b = 0; b < 19; b++)
			if(_units[b].preq == nextTechs[k])
				DrawStr(25, 23 + 8*a++, _units[b].name, A_NORMAL);
		if(a == c) DrawStr(25, 23 + 8*a++, "None", A_NORMAL);
		
		key = ngetchx();
		
		if(key == KEY_LEFT)
		{
			if(--k < 0) k = j - 1;
		}
		else if(key == KEY_RIGHT)
		{
			if(++k > j-1) k = 0;
		}
	}
	
	return nextTechs[k];
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// UpdateTechnology ///////////////////////////////////////////////////////////////////////////////////////////////
// updates the technology /////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void UpdateTechnology(char civ)
{
	if(civilization[civ].currentTechResearch == -1 || civilization[civ].researchAmount >= (techs[civilization[civ].currentTechResearch].epoch + 1) * TECH_COST)
	{
		civilization[civ].researchAmount = 0;
		if(civilization[civ].currentTechResearch != -1)
			SetBit(civilization[civ].techs, civilization[civ].currentTechResearch);
			
		civilization[civ].currentTechResearch = TechWindow(civ);
	}
}
